﻿// my_queue_stack.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <stack>
#include <queue>

using namespace std;

class MyQueue {
public:
    stack<int>cache_stack;
    stack<int>common_stack;

    MyQueue() {

    }

    void push(int x) {
        common_stack.push(x);
    }

    int pop() {
        while (!common_stack.empty())
        {
            cache_stack.push(common_stack.top());
            common_stack.pop();
        }

        int res = cache_stack.top();
        cache_stack.pop();

        while (!cache_stack.empty())
        {
            common_stack.push(cache_stack.top());
            cache_stack.pop();
        }

        return res;

    }

    int peek() {
        while (!common_stack.empty())
        {
            cache_stack.push(common_stack.top());
            common_stack.pop();
        }

        int res = cache_stack.top();
        while (!cache_stack.empty())
        {
            common_stack.push(cache_stack.top());
            cache_stack.pop();
        }
        return res;
    }

    bool empty() {
        return common_stack.empty();
    }


};



class MyStack {
public:
    queue<int>common_q;
    queue<int>cache_q;

    MyStack() {

    }

    void push(int x) {
        cache_q.push(x);
        while (!common_q.empty())
        {
            cache_q.push(common_q.front());
            common_q.pop();
        }
        swap(cache_q, common_q);
    }

    int pop() {
        int value = common_q.front();
        common_q.pop();
        return value;
    }

    int top() {
        return common_q.front();
    }

    bool empty() {
        return common_q.empty();
    }
};

int main()
{

    {
        //MyStack myStack;
        //// "push","pop","empty"
        //return 0;
    }

    //["MyQueue", "push", "push", "push", "push", "pop", "push", "pop", "pop", "pop", "pop"]
    //[[],[1],[2],[3],[4],[],[5],[],[],[],[]]
    MyQueue my_queue;
    my_queue.push(1); // queue is: [1]
    my_queue.push(2); // queue is: [1, 2] (leftmost is front of the queue)
    my_queue.push(3);
    my_queue.push(4);
    cout << my_queue.pop();
    my_queue.push(5);
    cout << my_queue.pop();
    cout << my_queue.pop();

}

// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单

// 入门使用技巧: 
//   1. 使用解决方案资源管理器窗口添加/管理文件
//   2. 使用团队资源管理器窗口连接到源代码管理
//   3. 使用输出窗口查看生成输出和其他消息
//   4. 使用错误列表窗口查看错误
//   5. 转到“项目”>“添加新项”以创建新的代码文件，或转到“项目”>“添加现有项”以将现有代码文件添加到项目
//   6. 将来，若要再次打开此项目，请转到“文件”>“打开”>“项目”并选择 .sln 文件
